//
//  main.m
//  TempTest
//
//  Created by yao zhaofeng on 2018/4/2.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import <Foundation/Foundation.h>

int maximumSwap(int num) {
    int arr[10] = {0};
    int temp = num, n = 0;
    while (temp) {
        arr[n++] = temp%10;
        temp = temp/10;
    }

    int i, j;
    for (i = n-1; i > 0; i--) {
        for(j = 0; j < i; j++) {
            if (arr[j] < arr[j+1]){
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    int result = 0;
    for(i = 0; i < n; i++){
        result = result*10 + arr[i];
    }
    
    return result;
}


double powerWithExponent(double base, int exponent) {
    //方法1:
//    int result = 1.0f;
//    for(int i = 0; i< exponent; i++)
//        result = result * base;
//
//    return result;
    
    //方法2:
    if (exponent == 0) {
        return 1;
    }
    if (exponent == 1) {
        return base;
    }
    
    double result = powerWithExponent(base, exponent >>1);
    result *= result;
    if((exponent&0x1) == 1)
        result *=base;
    return result;
}

double Power(double base, int exponent) {
    if (base>-0.0000001 && base<0.0000001 && exponent<0)
        return 0.0;
    
    double result = 1.0;
    
    unsigned int abs_exponent = (unsigned int) exponent;
    if(exponent < 0)
        abs_exponent = (unsigned int) (-exponent);
    
    //
    if(abs_exponent == 0)
        return 1.0;
    if(abs_exponent == 1)
        return base;
    
    result = powerWithExponent(base, abs_exponent);
    
    if(exponent < 0 && result > 0.0)
        result = 1.0 / result;
    return result;
}



int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        int a = 934984;
        printf("a = %d\n", a);
        int b = maximumSwap(a);
        printf("b = %d\n", b);
        double result = Power(10.0, 6);
        printf("result == %lf\n", result);
    }
    return 0;
}
